import Vue from 'vue'
import Router from 'vue-router'
import 'nprogress/nprogress.css'// Progress 进度条样式
import {getInfo} from "@/api/login"
import {Message} from 'element-ui'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
import NProgress from "nprogress";
import el from "element-ui/src/locale/lang/el";

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    hidden: true,
    meta: {
      requireAuth: true
    },
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {
        title: 'Dashboard', icon: 'dashboard'
        , requireAuth: true

      }
    }]
  },

  {
    path: '/user',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    alwaysShow: true,
    meta: {
      title: '用户管理', icon: 'el-icon-eleme'
      , requireAuth: true

    },
    children: [
      {
        path: 'list',
        name: 'userlist',
        component: () => import('@/views/animate/user/userlist'),
        meta: {
          title: '用户列表', icon: 'table', noCache: false
          , requireAuth: true

        }
      },
      {
        path: 'save',
        name: 'Table',
        hidden: true,
        component: () => import('@/views/animate/user/saveuser'),
        meta: {
          title: '用户添加', icon: 'table', noCache: false
          , requireAuth: true

        }
      },
      {
        path: 'edit',
        name: 'Edit',
        component: () => import('@/views/animate/user/edituser'),
        hidden: true,
        meta: {
          title: '用户修改', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'tree',
        name: 'Tree',
        hidden: true,
        component: () => import('@/views/tree/index'),
        meta: {title: 'Tree', icon: 'tree'}
      }
    ]
  },

// fenlei
  {
    path: '/category',
    component: Layout,
    alwaysShow: true,

    redirect: '/example/table',
    name: 'Example',
    meta: {
      title: '分类管理', icon: 'el-icon-mobile'
      , requireAuth: true

    },
    children: [
      {
        path: 'list',
        name: 'Table',
        component: () => import('@/views/animate/category/listcategory'),
        meta: {
          title: '分类列表', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'save',
        name: 'Table',
        hidden: true,
        component: () => import('@/views/animate/category/savecategory'),
        meta: {
          title: '分类添加', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'edit',
        name: 'Edit',
        component: () => import('@/views/animate/category/editcategory'),
        hidden: true,
        meta: {
          title: '分类修改', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'tree',
        name: 'Tree',
        hidden: true,

        component: () => import('@/views/tree/index'),
        meta: {
          title: 'Tree', icon: 'tree'
          , requireAuth: true

        }
      }
    ]
  },

//  商品分类
  {
    path: '/type',
    component: Layout,
    alwaysShow: true,

    redirect: '/example/table',
    name: 'Example',
    meta: {
      title: '商品分类管理', icon: 'el-icon-pie-chart'

      , requireAuth: true

    },
    children: [
      {
        path: 'list',
        name: 'Table',
        component: () => import('@/views/animate/type/listtype'),
        meta: {
          title: '商品分类列表', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'save',
        name: 'Table',
        hidden: true,
        component: () => import('@/views/animate/type/savetype'),
        meta: {
          title: '分类添加', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'edit',
        name: 'Edit',
        component: () => import('@/views/animate/type/edittype'),
        hidden: true,
        meta: {
          title: '分类修改', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'tree',
        name: 'Tree',
        hidden: true,

        component: () => import('@/views/tree/index'),
        meta: {title: 'Tree', icon: 'tree'}
      }
    ]
  },


//  商品
  {
    path: '/product',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    alwaysShow: true,
    meta: {
      title: '商品管理', icon: 'el-icon-house'
      , requireAuth: true

    },
    children: [
      {
        path: 'list',
        name: 'Table',
        component: () => import('@/views/animate/product/listproduct'),
        meta: {
          title: '商品列表', icon: 'table'

          , requireAuth: true
        }
      },
      {
        path: 'save',
        name: 'Table',
        hidden: true,
        component: () => import('@/views/animate/product/saveproduct'),
        meta: {
          title: '商品添加', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'edit',
        name: 'Edit',
        component: () => import('@/views/animate/product/editproduct'),
        hidden: true,
        meta: {
          title: '商品修改', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'tree',
        name: 'Tree',
        hidden: true,

        component: () => import('@/views/tree/index'),
        meta: {title: 'Tree', icon: 'tree'}
      }
    ]
  },
//  订单管理
  {
    path: '/order',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    meta: {
      title: '订单管理', icon: 'el-icon-shopping-cart-2'
      , requireAuth: true

    },
    children: [
      {
        path: 'list',
        name: 'Table',
        component: () => import('@/views/animate/order/listorder'),
        meta: {
          title: '订单列表', icon: 'table'
          , requireAuth: true

        }
      },


      {
        path: 'tree',
        name: 'Tree',
        component: () => import('@/views/tree/index'),
        meta: {
          title: 'Tree', icon: 'tree'
          , requireAuth: true

        }
      }
    ]
  },
  // 板块管理
  {
    path: '/plate',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    alwaysShow: true,
    meta: {
      title: '板块管理', icon: '\n' +
        'el-icon-time'
      , requireAuth: true

    },
    children: [
      {
        path: 'list',
        name: 'Table',
        component: () => import('@/views/animate/plate/platelist'),
        meta: {
          title: '板块列表', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'save',
        name: 'Table',
        hidden: true,
        component: () => import('@/views/animate/plate/saveplate'),
        meta: {
          title: '板块添加', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'edit',
        name: 'Edit',
        component: () => import('@/views/animate/plate/editplate'),
        hidden: true,
        meta: {
          title: '板块修改', icon: 'table'
          , requireAuth: true

        }
      },

      {
        path: 'tree',
        name: 'Tree',
        hidden: true,
        component: () => import('@/views/tree/index'),
        meta: {title: 'Tree', icon: 'tree'}
      }
    ]
  },


  // 字典管理
  {
    path: '/dict',
    component: Layout,
    redirect: '/example/table',
    name: 'Example',
    alwaysShow: true,
    meta: {
      title: '字典管理', icon: 'el-icon-sunny'
      , requireAuth: true

    },
    children: [
      {
        path: 'list',
        name: 'Table',
        component: () => import('@/views/animate/dict/listdict'),
        meta: {
          title: '字典列表', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'save',
        name: 'Table',
        hidden: true,
        component: () => import('@/views/animate/plate/saveplate'),
        meta: {
          title: '板块添加', icon: 'table'
          , requireAuth: true

        }
      },
      {
        path: 'edit',
        name: 'Edit',
        component: () => import('@/views/animate/plate/editplate'),
        hidden: true,
        meta: {
          title: '板块修改', icon: 'table'
          , requireAuth: true

        }
      },

      {
        path: 'tree',
        name: 'Tree',
        hidden: true,
        component: () => import('@/views/tree/index'),
        meta: {title: 'Tree', icon: 'tree'}
      }
    ]
  },

  {
    path: '/nested',
    component: Layout,
    redirect: '/nested/menu1',
    name: 'Nested',
    meta: {
      title: 'Nested',
      icon: 'nested'
    },
    hidden: true,
    children: [
      {
        path: 'menu1',
        component: () => import('@/views/nested/menu1/index'), // Parent router-view
        name: 'Menu1',
        meta: {title: 'Menu1'},
        children: [
          {
            path: 'menu1-1',
            component: () => import('@/views/nested/menu1/menu1-1'),
            name: 'Menu1-1',
            meta: {title: 'Menu1-1'}
          },
          {
            path: 'menu1-2',
            component: () => import('@/views/nested/menu1/menu1-2'),
            name: 'Menu1-2',
            meta: {title: 'Menu1-2'},
            children: [
              {
                path: 'menu1-2-1',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-1'),
                name: 'Menu1-2-1',
                meta: {title: 'Menu1-2-1'}
              },
              {
                path: 'menu1-2-2',
                component: () => import('@/views/nested/menu1/menu1-2/menu1-2-2'),
                name: 'Menu1-2-2',
                meta: {title: 'Menu1-2-2'}
              }
            ]
          },
          {
            path: 'menu1-3',
            component: () => import('@/views/nested/menu1/menu1-3'),
            name: 'Menu1-3',
            meta: {title: 'Menu1-3'}
          }
        ]
      },
      {
        path: 'menu2',
        component: () => import('@/views/nested/menu2/index'),
        name: 'Menu2',
        meta: {title: 'menu2'}
      }
    ]
  },

  {
    hidden: true,
    path: 'external-link',
    component: Layout,
    children: [
      {
        path: 'https://panjiachen.github.io/vue-element-admin-site/#/',
        meta: {title: 'External Link', icon: 'link'}
      }
    ]
  },

  // 404 page must be placed at the end !!!
  {path: '*', redirect: '/404', hidden: true}
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({y: 0}),
  routes: constantRoutes
})

const router = createRouter()

router.beforeEach((to, from, next) => {
  // console.log(to.matched[0].path=='/admin')
  if (to.meta.requireAuth) {  // 判断路由是否需要权限
    let token = localStorage.getItem('token')
    var type = localStorage.getItem('type');
    if (token &&type==1) {
      // var type = localStorage.getItem('type');
      // if (type == 1) {
      //   next()
      // } else {
      //   Message.error('非管理员用户')
      //   next({
      //     path: '/'
      //   })
      // }

      next()
    } else {
      Message.error('请先使用管理员账户登录登录')
      localStorage.removeItem('token')
      next({
        path: '/login',
        query: {redirect: to.path}
      })
    }
  }else {
    let token = localStorage.getItem('token')

    if (token){
      if (to.path === '/login') {
        Message.error('已经登录')
        next({
          path: '/'
        })
      }
    }
  }
  next()
})
// router.beforeEach((to, from, next) => {
//   NProgress.start()
//   var token = localStorage.getItem('token');
//   if (token) {
//     if (to.path === '/login') {
//       next({path: '/'})
//       Message.success('已经登录过了')
//       NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
//     } else {
//       getInfo(token).then(res => {
//         next()
//       }).catch(reason => {
//         Message.error(err || '验证失败，请重新登录')
//         next({path: '/'})
//       })
//
//     }
//   } else {
//     if (whiteList.indexOf(to.path) !== -1) {
//       next()
//     }
//     else {
//     // next({
//     //   path:'/login'
//     // })
//       next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
//       NProgress.done()
//     }
//   }
// })
// // 这里就是没登录
//
router.afterEach(() => {
  NProgress.done() // 结束Progress
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
